package com.addplus.townmall.server.api.mapper.town;

import com.addplus.townmall.server.api.model.town.CouponMemberRecord;
import com.addplus.townmall.server.api.model.town.expand.CouponMemberRecordExpand;
import com.addplus.townmall.server.api.utils.BaseAddMapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.math.BigDecimal;
import java.util.List;

/**
 * <p>
 * 优惠券商城会员记录表 Mapper 接口
 * </p>
 *
 * @author zhangjiehang
 * @since 2018-08-27
 */
public interface CouponMemberRecordMapper extends BaseAddMapper<CouponMemberRecord> {

    @Select("<script>\n" +
            "SELECT\n" +
            " r.id,\n" +
            " r.counpon_start_time,\n" +
            " r.counpon_end_time,\n" +
            " c.integral_cover,\n" +
            " c.coupon_name,\n" +
            " c.coupon_describe,\n" +
            " c.coupon_type,\n" +
            " c.discount_type,\n" +
            " c.store_id,\n" +
            " c.min_price,\n" +
            " c.sub_price,\n" +
            " c.discount, \n" +
            " c.platform_category_id_list, \n" +
            " c.goods_id_list \n" +
            "FROM\n" +
            " coupon_member_record r\n" +
            " LEFT JOIN coupon c ON c.id = r.coupon_id \n" +
            "WHERE\n" +
            " r.member_id = #{memberId}\n" +
            " <if test=\"type == 0\">\n" +
            "  AND r.is_use = 0 \n" +
            "   AND r.is_expire = 0 \n" +
            " </if>\n" +
            " <if test=\"type == 1\">\n" +
            "  AND r.is_use = 1 \n" +
            " </if>\n" +
            " <if test=\"type == 2\">\n" +
            "  AND r.is_expire = 1 \n" +
            " </if>\n" +
            " AND r.is_delete = 0 \n" +
            "</script>")
    List<CouponMemberRecordExpand> getCouponMemberRecordByType(Page page, @Param("memberId") Integer memberId, @Param("type") Integer type);


    @Select("SELECT\n" +
            " r.id,\n" +
            " r.counpon_start_time,\n" +
            " r.counpon_end_time,\n" +
            " c.integral_cover,\n" +
            " c.coupon_name,\n" +
            " c.coupon_describe,\n" +
            " c.coupon_type,\n" +
            " c.discount_type,\n" +
            " c.min_price,\n" +
            " c.sub_price,\n" +
            " c.discount,\n" +
            " c.platform_category_id_list,\n" +
            " c.goods_id_list \n" +
            "FROM\n" +
            " coupon_member_record r\n" +
            " LEFT JOIN coupon c ON c.id = r.coupon_id \n" +
            "WHERE\n" +
            " c.coupon_type IN ( 0, #{couponType} ) \n" +
            " AND (\n" +
            "  c.platform_category_id_list = '0' \n" +
            "  OR FIND_IN_SET( #{plarformType}, c.platform_category_id_list ) \n" +
            "  OR c.goods_id_list = '0' \n" +
            "  OR FIND_IN_SET( #{shopProductId}, c.goods_id_list ) \n" +
            " ) \n" +
            " AND r.is_use = 0 \n" +
            " AND r.is_delete = 0 \n" +
            " AND r.is_expire = 0 \n" +
            " AND r.member_id = #{memberId} \n" +
            "ORDER BY\n" +
            "\tc.sort,\n" +
            "\tc.gmt_create DESC")
    List<CouponMemberRecordExpand> getCouponByShopProductUser(@Param("memberId") Integer memberId, @Param("couponType") Integer couponType, @Param("plarformType") Integer plarformType, @Param("shopProductId") Integer shopProductId);

    @Select("SELECT\n" +
            " r.id,\n" +
            " r.counpon_start_time,\n" +
            " r.counpon_end_time,\n" +
            " c.integral_cover,\n" +
            " c.coupon_name,\n" +
            " c.coupon_describe,\n" +
            " c.coupon_type,\n" +
            " c.discount_type,\n" +
            " c.store_id,\n" +
            " c.min_price,\n" +
            " c.sub_price,\n" +
            " c.discount,\n" +
            " c.platform_category_id_list,\n" +
            " c.goods_id_list \n" +
            "FROM\n" +
            " coupon_member_record r\n" +
            " LEFT JOIN coupon c ON c.id = r.coupon_id \n" +
            " and c.online_status = 1\n" +
            "WHERE\n" +
            " c.store_id in (0 ,#{storeId})\n" +
            " AND c.min_price <= #{shopTotalAmount} \n" +
            " AND r.is_use = 0 \n" +
            " AND r.is_delete = 0 \n" +
            " AND r.is_expire = 0 \n" +
            " AND r.member_id = #{memberId} \n" +
            "ORDER BY\n" +
            " c.sort,\n" +
            " c.gmt_create DESC")
    List<CouponMemberRecordExpand> getCouponByShopProductNoLimitUser(@Param("memberId") Integer memberId, @Param("shopTotalAmount") BigDecimal shopTotalAmount, @Param("storeId") Integer storeId);

    @Select("SELECT\n" +
            " r.id,\n" +
            " r.counpon_start_time,\n" +
            " r.counpon_end_time,\n" +
            " c.integral_cover,\n" +
            " c.coupon_name,\n" +
            " c.coupon_describe,\n" +
            " c.coupon_type,\n" +
            " c.discount_type,\n" +
            " c.min_price,\n" +
            " c.sub_price,\n" +
            " c.discount,\n" +
            " c.platform_category_id_list,\n" +
            " c.goods_id_list \n" +
            "FROM\n" +
            " coupon_member_record r\n" +
            " LEFT JOIN coupon c ON c.id = r.coupon_id \n" +
            "WHERE\n" +
            " r.id=#{couponId} \n" +
            " AND r.is_use = 1 \n" +
            " AND r.is_delete = 0 \n" +
            " AND r.is_expire = 0 ")
    CouponMemberRecordExpand getSingleCouponByShopProduct(@Param("couponId") Integer couponMemberId);


}
